﻿using Logistics.Car;
using Microsoft.AspNetCore.Mvc;
using System;

namespace Logistics.Web.Controllers
{
    /// <summary>
    /// 线路
    /// </summary>
    public class LineController : Controller
    {
        private readonly ILineAppService _VehicleAppService;
        public LineController(ILineAppService VehicleAppService)
        {
            _VehicleAppService = VehicleAppService;
        }
        public IActionResult Index()
        {
            return View();
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <returns></returns>
        public IActionResult PostItemView()
        {
            return View();
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public IActionResult Edit(int Id)
        {
            ViewBag.Id = Id;
            return View();
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public IActionResult Delete(int Id = 0)
        {
            var ok = _VehicleAppService.DeleteAsync(Id);
            return Ok(ok);
        }
        /// <summary>
        /// 显示
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        public IActionResult GetFieldList(int page = 1, int limit = 3)
        {


            var getFoodListDto = new GetLineDto
            {
                SkipCount = (page - 1) * limit,
                MaxResultCount = limit,
                Sorting = "Id desc"
            };
            var foods = _VehicleAppService.GetListAsync(getFoodListDto).Result;
            return Ok(new { data = foods.Items, count = foods.TotalCount, code = "0", msg = "" });

        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="cd"></param>
        /// <returns></returns>
        public IActionResult UpdateFieId(LineDto cd)
        {
            cd.Ldate = DateTime.Now;
            var result = _VehicleAppService.Update(cd).Result;
            if (result > 0)
            {
                return Redirect("/Line/Index");
            }
            else
            {
                return Redirect("/Vehicle/Edit?Id=" + cd.Id);
            }
        }
        /// <summary>
        /// 反填
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public IActionResult GetCarryById(int Id)
        {
            var list = _VehicleAppService.GetCarryById(Id).Result;
            return Ok(list);
        }
        public ActionResult CreateLine(LineDto cd)
        {

            cd.Ldate = DateTime.Now;
            var result = _VehicleAppService.Create(cd);
            if (result.Result != null)
            {
                return Redirect("/Line/Index");
            }
            else
            {
                return Content("<script>layer.msg('添加失败');location.reload()</script>");
            }
        }
    }
}
